package definite.Guide.Book.JColorChooser;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.colorchooser.AbstractColorChooserPanel;
import javax.swing.colorchooser.ColorSelectionModel;

import definite.Guide.Book.ToggleButton.DiamondIcon;

public class CustomAbstractColorChooserPanel extends AbstractColorChooserPanel
		implements ItemListener {

	private static int NOT_FOUND = -1;
	private JComboBox comboBox;

	private String labels[] = { "BLACK", "BLUE", "CYAN", "DARK_GRAY", "GRAY",
		"GREEN", "LIGHT_GRAY", "MAGENTA", "ORANGE", "PINK", "RED", "WHITE",
		"YELLOW", "activeCaption", "activeCaptionBorder", "activeCaptionText",
		"control", "controlDkShadow", "controlHighlight", "controlLtHighlight",
		"controlShadow", "controlText", "desktop", "inactiveCaption",
		"inactiveCaptionBorder", "inactiveCaptionText", "info", "infoText",
		"menu", "menuText", "scrollbar", "text", "textHighlight",
		"textHighlightText", "textInactiveText", "textText", "window",
		"windowBorder", "windowText", "<Custom>" };

	private Color colors[] = { Color.BLACK, Color.BLUE, Color.CYAN,
		Color.DARK_GRAY, Color.GRAY, Color.GREEN, Color.LIGHT_GRAY,
		Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE,
		Color.YELLOW, SystemColor.activeCaption,
		SystemColor.activeCaptionBorder, SystemColor.activeCaptionText,
		SystemColor.control, SystemColor.controlDkShadow,
		SystemColor.controlHighlight, SystemColor.controlLtHighlight,
		SystemColor.controlShadow, SystemColor.controlText,
		SystemColor.desktop, SystemColor.inactiveCaption,
		SystemColor.inactiveCaptionBorder, SystemColor.inactiveCaptionText,
		SystemColor.info, SystemColor.infoText, SystemColor.menu,
		SystemColor.menuText, SystemColor.scrollbar, SystemColor.text,
		SystemColor.textHighlight, SystemColor.textHighlightText,
		SystemColor.textInactiveText, SystemColor.textText, SystemColor.window,
		SystemColor.windowBorder, SystemColor.windowText, null };

	// change combobox to match color if possible
	private void setColor(Color color) {
		int pos = findColorPos(color);
		comboBox.setSelectedIndex(pos);
	}

	// give a label , find its possition in the labels list
	private int findLabelPos(Object Label) {
		int pos = NOT_FOUND;
		String label = Label.toString();
		for (int i = 0; i < labels.length; i++)
			if (labels[i] != null && labels[i].equals(label)) {
				pos = i;
				break;
			}
		return pos;
	}

	// give a color , find its possition in the colors list
	private int findColorPos(Color color) {
		int pos = colors.length - 1;
		int colorRGB = color.getRGB();
		// can't use equal() to compare Color and SystemColor
		for (int i = 0; i < colors.length; i++)
			if (colors[i] != null && colors[i].getRGB() == colorRGB) {
				pos = i;
				break;
			}
		return pos;
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		int state = e.getStateChange();
		if (state == ItemEvent.SELECTED) {
			int pos = findLabelPos(e.getItem());
			// not found or last possition(not choose) is ignored
			if (pos != NOT_FOUND && pos != labels.length - 1) {
				ColorSelectionModel selectionModel = getColorSelectionModel();
				selectionModel.setSelectedColor(colors[pos]);
			}
		}
	}

	@Override
	public void updateChooser() {
		Color color = getColorFromModel();
		setColor(color);
	}

	@Override
	protected void buildChooser() {
		comboBox = new JComboBox(labels);
		comboBox.addItemListener(this);
		add(comboBox);
	}

	@Override
	public String getDisplayName() {
		return "SystemColor";
	}

	@Override
	public Icon getSmallDisplayIcon() {

		return new DiamondIcon(Color.BLUE);
	}

	@Override
	public Icon getLargeDisplayIcon() {

		return new DiamondIcon(Color.GREEN);
	}

	// run app
	public static void main(String[] args) {
		Runnable r = new Runnable() {

			@Override
			public void run() {
				JFrame.setDefaultLookAndFeelDecorated(true);
				JFrame frame = new JFrame("Customize JColorChooser Panel");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				final JButton button =
					new JButton("Click to changed background");
				// defind action listener
				ActionListener action = new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						Color oldBackGround = button.getBackground();
						final JColorChooser chooser =
							new JColorChooser(oldBackGround);
						CustomAbstractColorChooserPanel choosePanel =
							new CustomAbstractColorChooserPanel();

						// by this way , replaced default choose panel to the
						// new choosePanel

						/*
						  AbstractColorChooserPanel choosePanels[] =
							{ choosePanel };
						chooser.setChooserPanels(choosePanels);*/

						// by this way , add choosePanel to the default choose
						// panel
						chooser.addChooserPanel(choosePanel);

						// code to customize the preview panel
						JLabel view =
							new JLabel("The Color Change", JLabel.CENTER);
						view.setFont(new Font("San-Serif", Font.BOLD
							| Font.ITALIC, 50));
						view.setSize(view.getPreferredSize());
						chooser.setPreviewPanel(view);
						// end of customize preview panel

						// for OK button , changed background for the button
						ActionListener okAction = new ActionListener() {

							@Override
							public void actionPerformed(ActionEvent e) {
								Color newColor = chooser.getColor();
								if (newColor.equals(button.getForeground()))
									JOptionPane.showMessageDialog(null,
											"Cannot Changed background color "
												+ "the same with foreground",
											"Color Changed Rejected",
											JOptionPane.ERROR_MESSAGE);
								else
									button.setBackground(newColor);
							}
						};

						// cancel -- changed background to green
						ActionListener cancelAction = new ActionListener() {

							@Override
							public void actionPerformed(ActionEvent e) {
								button.setBackground(Color.GREEN);
							}
						};
						// end action

						final JDialog dialog =
							JColorChooser.createDialog(null,
									"Customize JColorChooser Panel", true,
									chooser, okAction, cancelAction);

						Runnable showDialog = new Runnable() {

							@Override
							public void run() {
								dialog.setVisible(true);
							}
						};
						EventQueue.invokeLater(showDialog);
					}
				};
				button.addActionListener(action);
				frame.add(button, BorderLayout.CENTER);
				frame.setLocationRelativeTo(null);
				frame.setSize(300, 100);
				frame.setVisible(true);
			}
		};

		EventQueue.invokeLater(r);
	}
}
